/*
 * @Author: Lee 497232807@qq.com
 * @Date: 2023-07-18 09:43:45
 * @LastEditors: Lee 497232807@qq.com
 * @LastEditTime: 2023-08-07 09:27:35
 * @FilePath: \hotDemon\assets\script\view\loadScene.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { _decorator, Component, director, game, Label, native, Node, ProgressBar } from 'cc';
import { StateCode, VersionManager } from '../common/VersionManager';
import { NATIVE } from 'cc/env';
const { ccclass, property } = _decorator;

@ccclass('loadScene')
export class loadScene extends Component {

    /**
     * 热更新状态
     */
    @property(Label)
    lb_hot_status: Label = null;
    /**
     * 热更新进度条
     */
    @property(ProgressBar)
    progress_hot: ProgressBar = null;

    start() {
        this.checkUpdate();
    }

    /**
     * 检查更新
     */
    protected checkUpdate() {
       
        if(NATIVE)
        {
            let url = "http://192.168.0.158/publish/remoteConfig"
            var gameConfig = "http://192.168.0.158/publish/gameConfig"
            VersionManager.Instance.parseRemoteSubGameInfo(gameConfig);
            VersionManager.Instance.checkUpdate(url,(code:StateCode)=>{
                console.log("热更新状态:",code)
                if(code == StateCode.UPDATE_SUCCESS){ //更新成功
                    game.restart();
                }
                else if(code == StateCode.NO_NEED_UPDATE)//不用更新
                {
                    
                    this.goMainScene()
                }
                else if(code == StateCode.FORCE_UPDATE) //强制更新安装包
                {
                    this.lb_hot_status.string = "强制更新安装包";
                }
                else
                {
                    this.lb_hot_status.string = `热更新失败:${code}`
                } 
            },(downSize:number,totalSize:number)=>{
                
                var progress = downSize/totalSize
                this.progress_hot.node.active = true
                this.progress_hot.progress = progress;
                console.log("热更新进度:",progress)
            })
        }
        else
        {
            this.goMainScene()
        }
        
    }

    protected goMainScene() {
        director.loadScene("main");
    }

}

